/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.anniversary.repository;

import me.zhengjie.modules.anniversary.domain.Activities;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;

import java.util.List;
import java.util.Map;

/**
* @website https://el-admin.vip
* @author Jiayin Wei
* @date 2020-07-28
 *
**/
public interface ActivitiesRepository extends JpaRepository<Activities, Long>, JpaSpecificationExecutor<Activities> {

    @Query(value = "select a.* from gzmu_ann_app_user_info i,sys_user u,gzmu_ann_activities a where i.user_id=u.user_id and i.activities_id=a.id and i.user_id=:userId", nativeQuery = true)
    List<Map<String, Object>> getAssociationsByType(Long userId, Pageable pageable);

    @Query(value = "select count(au.id) number,a.avatar_url,a.id,a.name,a.photo_path,a.start_time,a.end_time,a.place,a.create_by,a.content from gzmu_ann_activities a left join gzmu_ann_app_user_info au on au.activities_id=a.id and au.is_enable=1 WHERE a.id=?1", nativeQuery = true)
    List<Map<String, Object>> getActivitiesById(Long activitiesId);

    @Query(value = "select * from gzmu_ann_activities", nativeQuery = true)
    List<Map<String, Object>> getActivities(Pageable pageable);

    @Query(value = "select * from gzmu_ann_activities where name like %:name%", nativeQuery = true)
    List<Map<String, Object>> getActivitiesByname(String name, Pageable pageable);
    /**
     * 查找首页模块最新3条校庆活动信息
     * @param pageable
     * @return
     */
    @Query(value = "select * from gzmu_ann_activities WHERE verify_status=1 AND is_enable=1 ORDER BY sort DESC", nativeQuery = true)
    List<Map<String,Object>> findByActivities(Pageable pageable);

    @Query(value = "select a.id,a.name,a.content,u.name createName, a.start_time,a.end_time,a.photo_path from gzmu_ann_activities a left join sys_user u on u.id=a.user_id where a.user_id=1 and a.is_enable=1 ORDER BY start_time desc limit 0,1",nativeQuery = true)
    List<Map<String,Object>> queryActivityNewsOne(Long id);
}
